<?

class MOD_CONSTRUCT extends ENGINE {
	function __construct( $json = false ) {
		
		if( $json ) $this->json = $json;
	}
	
	var $cache = array();

/*	Retreive column of mods	*/	
	public function filter( $what ) {
		
		if( $column = $this->json->{ $what })
			foreach( $column as $node )
				$results[] =  $this->get( (int) $node );

		if( $results ) return implode( "\n", $results );
	}
	
/*	Retreive column of mods	*/	
	public function filter_array( $what ) {
		
		$results = array();
		
		if( $column = $this->json->{ $what })
			foreach( $column as $node )
				if( is_array( $result = $this->get( (int) $node ) ))
				
					array_merge( $results, $result );
				
				else $results[] =  $result;

		if( $results ) return $results;
	}
	
/*	Retreive column of mods for CMS	*/	
	public function filter_core( $what ) {
		
		if( $column = $this->json->{ $what })
			foreach( $column as $node )
				$results[] =  $this->render_core_json( (int) $node, $what );

		if( $results ) return implode( ",", $results );
	}

/*	Retreive single mod	*/	
	public function get( $id ) {
		
		return ( $MOD = $this->call( $id ))?
			$MOD->output() : "Empty.";
	}

/*	Create new mod */	
	function create( $mod, $column = false ) {
		global $DB, $Engine;
		
		$DB->query( "insert into moddump( `type`, json, llog, active, author ) values ( '{$DB->type( "module" )}', '{\"mod\":\"$mod\"}', now(), 1 , '" . $Engine->user_get( 'ID' ) . "' )" );
		$this->id = (int) $DB->query_direct( "select max(ID) from moddump" );
		
		if( $column )
			$this->attach( $column );
		
		return $this->request_json( $this->id );
	}


/*	Call (create) mod class */
	public function call ( $id ) {
		
		if( $id && $vars = $this->request_json( $id )) {
			$vars->modid = $id;
			return $this->render( $vars );
		}
	}
	
/*	Render mod class */
	public function render ( $vars ) {
		global $Page;
		
		if( $vars->modclass = $this->req_mod( $vars->mod ))
			return new $vars->modclass( $vars );
						
		else return "Error.";
	}
	
/*	Include mod class */
	public function req_mod ( $mod ) {
		
		$modclass	= ( $mod )? $this->camelize( $mod ) : false;
		
		if( !class_exists( $modclass, false ))
			require_once( ( file_exists ($custom = ROOT . ACCOUNT ."/custom.root/modules/$mod/module.php"))?
						$custom : ENGINE_CORE . "modules/$mod/module.php" );
		
		return $modclass;
	}
	
/*	Render mod json for CMS */
	public function render_core_json ( $id, $column = false ) {
		global $Engine, $Page;
		
		$vars = $this->request_json( $id );
		$name = ( $vars->nameid )? $Engine->text->get( $vars->nameid ): "module";
		
		$object = array( "id"=> $id, "type"=> "module", "name"=> $name, "descr"=> $vars->mod, "list"=> array( $name, $vars->mod ));
		if( $column ) $object[ "page" ] = array( "id"=> (int) $Page->get( 'ID' ), "column"=> $column );
		
		return json_encode( $object );
	}

/*	Retreive json object */
	public function request_json( $id ) {
		global $DB;
		
		if( !$this->cache[ $id ] ) $this->cache[ $id ] = $DB->query_direct( "select json from moddump where ID='$id'" );
		
		$json = json_decode( $this->cache[ $id ] );
		$json->id = $id;
		
		return $json;
	}
	
/*	Retreive object object */
	public function request_object( $id ) {
		global $DB;
		
		return new TEMP_MOD_EXTEND( $id );
	}


	
/*	Check for pre-loops */		
	function construct_check() {
		global $Page;
		
		if( $construct = $Page->get('meta')->construct )
			foreach( $construct as $constr_id )
				$this->call( $constr_id )->construct();
	}

/*	Retreive unique ids */	
	public function ids() {
		global $Page;
		
		if( !isset( $this->modids ) && $this->json )
			foreach( $this->json as $column )
				if( count($column) ) foreach( $column as $mod ) $this->modids[ $mod ] = $mod;
				
		if( $preload = $Page->get( 'preload' ))
			foreach( $preload as $mod ) $this->modids[ $mod ] = $mod;
		
		return $this->modids;
	}


	
/** 	SET FUNCS
* Functions setting values in page cache and/or DB
* 												 */
	function set( $id, $param ) {
		
		// WHAT TO DO WITH THIS?
		echo "Function not set.";
	}
	
	
	function attach( $column ) {
		global $Page;
		
		$mods = $Page->get( 'mods' );
		$mods->{ $column }[] = $this->id;
		
		$Page->set( "mods", json_encode( $mods ));
	}
}

class TEMP_MOD_EXTEND {

	function __construct( $id ) {
		
#		Load assigned object
		$this->load( $this->id = (int) $id );

#		Set up media Class (to do for module)
		//$this->media = new OBJECT_MEDIA( $this->type, $this->values[ 'ID' ]);
	}
	
	public function load( $id ) {
		global $DB;
		
#		Get DB row & decode meta
		$raw = $DB->query( "select * from moddump where ID=$id" );	
		$this->values = mysql_fetch_array( $raw, MYSQL_ASSOC);
		$this->values [ 'json' ] = json_decode( $this->values[ 'json' ]);
				
		return $this->values;
	}

	public function set( $key, $param ) {
		global $DB;
		
		if( $this->values[ $key ] )
			return "Function not fully set yet";
		
		else $this->values[ 'json' ]->{ $key } = $param;
		
		$DB->query( $text = "update moddump set json='".json_encode( $this->values[ 'json' ] )."' where ID=" . $this->id, 0 );
	}
	
}


?>